package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.domain.MarketOrderExample;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class AdminOrderServiceImpl implements AdminOrderService {

    @Override
    public List<MarketOrder> List(String page, String limit, String sort, String order, String orderStatusArray, String start, String end, String userId, String orderSn) {
        SqlSession session = MybatisUtils.getSession();
        MarketOrderMapper orderMapper = session.getMapper(MarketOrderMapper.class);
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        if (!StringUtils.isEmpty(orderStatusArray)){
            criteria.andOrderStatusEqualTo(Short.valueOf(orderStatusArray));
        }
        if (!StringUtils.isEmpty(start)&&!StringUtils.isEmpty(end)){
            criteria.andAddTimeBetween(LocalDateTime.parse(start),LocalDateTime.parse(end));
        }
        if (!StringUtils.isEmpty(userId)){
            criteria.andIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(orderSn)){
            criteria.andOrderSnEqualTo(orderSn);
        }
        orderExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketOrder> marketOrderList=orderMapper.selectByExample(orderExample);
        session.close();
        return marketOrderList;
    }
}
